<!--
 * @Description: 
 * @Author: chenjiaxin
 * @Date: 2022-03-19 09:24:20
 * @LastEditTime: 2022-03-19 11:02:43
 * @FilePath: \WebSkillExtend\eg12-Array.reducer\index.html
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Array.reducer</title>
  </head>
  <body>
    <script>
      /**
       * 第二个参数：起始值 作为第一次的 previous。
       */
      // const array = [15, 12, 12, 10];

      // function reducer(previous, current, index) {
      //   const returns = previous + current;
      //   console.log(
      //     `previous: ${previous}, current: ${current}, index: ${index}, returns: ${returns}`
      //   );
      //   return returns;
      // }

      // let result = array.reduce(reducer);
      // console.log(result);

      // 使用 reduce 替换 filter过滤
      const numbers = [-5, 6, 12];

      const doubledPositiveNumbers = numbers.reduce((previous, current) => {
        console.log(previous);
        if (current > 0) {
          const doubled = current * 2;
          previous.push(doubled);
        }
        return previous;
      }, []);
      console.log(doubledPositiveNumbers);
    </script>
  </body>
</html>
